var name;

$(document).ready(function(){
	imagenes();

	$('#topnavi li').click(function(){
		$('#topnavi li').removeClass('select');
		$(this).addClass('select');
	});
	$('#headimg').jqFancyTransitions({ 
		effect: 'wave', // wave, zipper, curtain
		width: 900, // width of panel
		height: 332, // height of panel
		strips: 20, // number of strips
		delay: 5000, // delay between images in ms
		stripDelay: 50, // delay beetwen strips in ms
		titleOpacity: 0.7, // opacity of title
		titleSpeed: 1000, // speed of title appereance in ms
		position: 'alternate', // top, bottom, alternate, curtain
		direction: 'fountainAlternate', // left, right, alternate, random, fountain, fountainAlternate
		navigation: false, // prev and next navigation buttons
		links: false // show images as links

	});
	
	$('.estrella').click(function(){
		var path=window.location.pathname;
		var k=path.indexOf('/films/view/1');
		path=path.substring(0,k+1);
		var i=parseInt($(this).attr('id').substring(5));
		for(var j=0;j<=i;j++){
			if(j%2==0)
				$('#calif'+j).attr('src',path+'img/HalfStarLeft.png');
			else
				$('#calif'+j).attr('src',path+'img/HalfStarRight.png');
		}
		for(var j=i+1;j<=10;j++){
			if(j%2==0)
				$('#calif'+j).attr('src',path+'img/HalfStarGrayLeft.png');
			else
				$('#calif'+j).attr('src',path+'img/HalfStarGrayRight.png');
		}
		$('#calif').attr('value',i+1);	
	});
	
	$( "#accordion" ).accordion({
		header: 'h3',
		collapsible: true,
		autoHeight: false
	});
	$('#nuevo-tag').click(function(){
		$( "#dialog-tag" ).dialog( "open" );
	});
		
	$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 'auto',
			width: '800',
			modal: true
		
			//$( this ).dialog( "close" );
		});

		$( "#dialog-related" ).dialog({
			autoOpen: false,
			height: 'auto',
			width: 'auto',
			modal: true
		
			//$( this ).dialog( "close" );
		});
var tag = $( "#new-tag" );		
	$( "#dialog-tag" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			buttons: {
				"Add new Tag": function() {
					var bValid = true;
					
					if(tag.val().length<1){
						bValid = false;
						$('#validateTips').css({display:'block'});
					}

					if ( bValid ) {
						$( "#FilmNewTags" ).attr('value',$( "#FilmNewTags" ).attr('value')+tag.val()+'     ');
						$('#validateTips').css({display:'none'});
						$( this ).dialog( "close" );
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				tag.val( "" ).removeClass( "ui-state-error" );
			}
		});
	
});

function imagenes(){
	var p=window.location.pathname;
	var l=p.indexOf('/films');
	if(l==-1){
		l=p.indexOf('/actors');
		if(l==-1){
			l=p.indexOf('/directors');
			if(l==-1){
				l=p.indexOf('/search');
				if(l==-1){
					l=p.indexOf('/directors');
					if(l==-1){
						l=p.indexOf('/info');
						if(l==-1){
							l=p.indexOf('/Users');
							
								
						}
					}
				}
			}
		}
	}
	if(l!=-1)
		p=p.substring(0,l+1);
	$('#img1').attr('src',p+'img/img1.jpg');
	$('#img2').attr('src',p+'img/img2.jpg');
	$('#img3').attr('src',p+'img/img3.jpg');
	$('#img4').attr('src',p+'img/img4.jpg');
	$('#img5').attr('src',p+'img/img5.jpg');
	

}
function checkLength( o, n, min ) {
	if (o.val().length < min ) {
		o.addClass( "ui-state-error" );
		updateTips( "Length of " + n + " must be at least 1");
		return false;
	} else {
		return true;
	}
}

function calificacion(calificacion){
	var c=parseInt(calificacion);
	for(var j=0;j<c;j++){
		if(j%2==0)
			$('#c'+j).attr('src','/Proyecto4IAW/img/HalfStarLeft.png');
		else
			$('#c'+j).attr('src','/Proyecto4IAW/img/HalfStarRight.png');
	}
	for(var j=c;j<=10;j++){
		if(j%2==0)
			$('#c'+j).attr('src','/Proyecto4IAW/img/HalfStarGrayLeft.png');
		else
			$('#c'+j).attr('src','/Proyecto4IAW/img/HalfStarGrayRight.png');
	}
}

function nuevo(form){
	var i=0;
	$('#dialog-form').empty();
	$('#dialog-form').append(form);
	$('#dialog-form').css({'text-align': 'left'});
	$('#dialog-form').dialog('open');
	
}
function nuevo_actor(i){
	if(parseInt(i)!=-1){//actor agregado
		$('#dialog-form').dialog( "close" );
		
		$('#select-actors').append('<div class="check"><input type="checkbox" name="data[Actor][Actor][]" value="'+i+'" id="ActorActor'+i+'" checked="checked"><label for="ActorActor'+i+'">'+name+'</label></div>');
	
	}else{
		$('#div-name-actor').addClass('error');
		alert('Error en el ingreso de datos');
		
	}
}
function nuevo_director(i){
	if(parseInt(i)!=-1){//actor agregado
		$('#dialog-form').dialog( "close" );
		
		$('#select-directors').append('<div class="check"><input type="checkbox" name="data[Director][Director][]" value="'+i+'" id="DirectorDirector'+i+'" checked="checked"><label for="DirectorDirector'+i+'">'+name+'</label></div>');
	
	}else{
		$('#div-name-director').addClass('error');
		alert('Error en el ingreso de datos');
		
	}
}
function nuevo_genero(i){
	if(parseInt(i)!=-1){//actor agregado
		$('#dialog-form').dialog( "close" );
		
		$('#select-genres').append('<div class="check"><input type="checkbox" name="data[Genre][Genre][]" value="'+i+'" id="GenreGenre'+i+'" checked="checked"><label for="GenreGenre'+i+'">Accion</label></div>');
	
	}else{
		$('#div-name-genre').addClass('error');
		alert('Error en el ingreso de datos');
		
	}
}
function nuevo_tag(i){
	if(parseInt(i)!=-1){//actor agregado
		$('#dialog-form').dialog( "close" );
		
		$('#select-tags').append('<div class="check"><input type="checkbox" name="data[Director][Director][]" value="'+i+'" id="DirectorDirector'+i+'" checked="checked"><label for="DirectorDirector'+i+'">'+name+'</label></div>');
	
	}else{
		$('#div-tag').addClass('error');
		alert('Error en el ingreso de datos');
		
	}
}


function searchCallback(data) {
	$("#post2").html("")
	var cadena="";
	var obj = jQuery.parseJSON(data);
	if (obj[0].total > 0){
		$('#FilmTitle').val(obj[0].titulo);
		$('#FilmSynopsis').val(obj[0].synopsis);
		$('#FilmDuration').val(obj[0].runtime);
		$('#FilmReleaseDateYear').val(obj[0].year);
		$('#FilmImage').val(obj[0].imagen);
		$('#FilmCalificacionTotal').val(obj[0].calificacion);
		
	
		var inputs='';
		i=0;
/*		$.each(obj[2], function(index, movie) {
			
		}*/
	/*	$.each(obj[1], function(index, movie) {
			
		//	inputs=inputs+'<input type="checkbox" name="'+i+'"'+' value="'+movie.id+'">'+movie.title+'</input>';
			i++;	
			//cadena=cadena+ "=" +movie.title;
			});
	//	inputs='<form method="post" action="adds">'+inputs+'<input type="submit">add films</input></form>';
	//	$('#dialog-related').append(inputs);
		//$( "#dialog-related" ).dialog('open');*/
		}
	else{
		$('#FilmSynopsis').val("");
		$('#FilmDuration').val("");
		$('#FilmReleaseDateYear').val("2012");
		$('#FilmImage').val("");
		$('#RFilmRFilm').val("");
	}
}